//
//  BillCell1.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/9/5.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "BillCell1.h"

#import "DatePickerPopView.h"

@interface BillCell1()<DatePickerPopViewDelegate>
@property(nonatomic,strong)UIView * line;

@property(nonatomic,strong)UILabel * mainLabel;

@property(nonatomic,strong)UILabel * subLabel;

@property(nonatomic,strong)UIImageView * icon;

@property(nonatomic,strong)DatePickerPopView * datePicker;

@end
@implementation BillCell1

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.cellHeight = Bill_height(45);
        UIImageView * backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(globalMargin, 0, ScreenWidth-2*globalMargin, self.cellHeight)];
        [self.contentView insertSubview:backgroundImageView atIndex:0];
        self.backgroundImageView = backgroundImageView;
        UIView * line = [[UIView alloc] initWithFrame:CGRectMake(2*globalMargin, self.cellHeight - 1, ScreenWidth-4*globalMargin, 1)];
        line.backgroundColor =HexColor(@"#EEEEEE");
        [self.contentView addSubview:line];
        self.line = line;
        self.enableUnderLine = NO;
        
        
        self.mainLabel = [[UILabel alloc] init];
        self.mainLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.mainLabel];
        
        self.subLabel = [[UILabel alloc] init];
        self.subLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:self.subLabel];
        
        self.icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Bill_width(20), Bill_width(20))];
        self.icon.userInteractionEnabled = YES;
        [self.icon addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick:)]];
        [self addSubview:self.icon];
    }
    return self;
}

-(void)setBackgroundImage:(UIImage *)backgroundImage{
    _backgroundImage = backgroundImage;
    self.backgroundImageView.image = _backgroundImage;
    [self setNeedsLayout];
}


-(void)setEnableUnderLine:(BOOL)enableUnderLine{
    _enableUnderLine = enableUnderLine;
    self.line.hidden = !_enableUnderLine;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    self.line.size = CGSizeMake(ScreenWidth-4*globalMargin, 0.5);
    self.line.x = 2*globalMargin;
    self.line.y = self.cellHeight-1;
    
    self.mainLabel.size = [self.mainLabel.text sizeInAre:MaxSize font:self.mainLabel.font];
    self.mainLabel.x = 2*globalMargin;
    self.mainLabel.centerY = self.cellHeight * 0.5;
    
    
    self.icon.x = ScreenWidth - self.icon.width-2*globalMargin;
    self.icon.centerY = self.mainLabel.centerY;
    
    self.subLabel.size = [self.subLabel.text sizeInAre:MaxSize font:self.subLabel.font];
    self.subLabel.x = self.icon.x - self.subLabel.width-globalMargin;
    self.subLabel.centerY = self.mainLabel.centerY;
    
}

-(void)setMainTitle:(NSString *)mainTitle subTitle:(NSString *)subTitle icon:(UIImage *)image{
    self.mainLabel.text = mainTitle;
    self.subLabel.text = subTitle;
    self.icon.image = image;
    if(!image){
        self.icon.size = CGSizeZero;
    }
    else{
        self.icon.size = CGSizeMake(Bill_width(20), Bill_width(20));
    }
    [self setNeedsLayout];
}

-(void)iconClick:(UITapGestureRecognizer*)gesture{
    [self.datePicker show];
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated{
    
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}

-(DatePickerPopView*)datePicker{
    if(_datePicker == nil){
        UIWindow * w = [[UIApplication sharedApplication].delegate window];
        CGRect rect = [self.icon convertRect:self.frame toView:w];
        CGPoint p = rect.origin;
        _datePicker = [[DatePickerPopView alloc] initWithFrame:CGRectMake(0, p.y-5, Bill_width(108), Bill_height(96))];
        _datePicker.delegate = self;
        _datePicker.x = ScreenWidth - _datePicker.width - globalMargin;
    }
    return _datePicker;
}

-(void)DatePickerPopView:(DatePickerPopView *)datePickerPopView didSelectedDate:(NSString *)date{
    self.mainLabel.text = [NSString stringWithFormat:@"%@/2016",date];
}
@end
